package com.qumitech.visualnovel.service.gamerecord.impl;

import java.util.*;

import com.qumitech.visualnovel.service.gamerecord.constant.TblNameFormatEnum;
import com.qumitech.visualnovel.service.gamerecord.util.TermGameTableNameUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;

import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.gamerecord.GameRecordService;
import com.qumitech.visualnovel.model.dao.app.Account;
import com.qumitech.visualnovel.model.dao.console.GameRecordDO;
import com.qumitech.visualnovel.model.dao.app.UserGamingWishBottleStatisticsDO;
import com.qumitech.visualnovel.pubdef.dto.game.GameDailyDataDTO;
import com.qumitech.visualnovel.model.dao.console.MuchangwuyuDO;
import com.qumitech.visualnovel.model.dao.app.UserGamingMuchangwuyuStatisticsDO;
import com.qumitech.visualnovel.model.dao.console.MuchangwuyuTermDO;
import com.qumitech.visualnovel.model.dao.console.UserPropExchangeDO;

import com.qumitech.visualnovel.datasource.console.GameRecordDOMapper;
import com.qumitech.visualnovel.datasource.console.MuchangwuyuDOMapper;
import com.qumitech.visualnovel.datasource.console.MuchangwuyuTermDOMapper;
import com.qumitech.visualnovel.datasource.console.UserPropExchangeDOMapper;
import com.qumitech.visualnovel.datasource.app.AccountMapper;
import com.qumitech.visualnovel.datasource.app.UserGamingWishBottleStatisticsDOMapper;
import com.qumitech.visualnovel.datasource.app.UserGamingMuchangwuyuStatisticsDOMapper;
import com.qumitech.visualnovel.datasource.app.GamingMuchangwuyuBaseCfgDOMapper;

@Service
public class GameRecordServiceImpl implements GameRecordService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String PLAYER_LIST_URL = "/account/admin/information";

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private GameRecordDOMapper gameRecordDOMapper;

    @Autowired
    private MuchangwuyuDOMapper muchangwuyuDOMapper;

    @Autowired
    private MuchangwuyuTermDOMapper muchangwuyuTermDOMapper;

    @Autowired
    private UserPropExchangeDOMapper userPropExchangeDOMapper;

    @Autowired
    private UserGamingWishBottleStatisticsDOMapper userGamingWishBottleStatisticsDOMapper;

    @Autowired
    private UserGamingMuchangwuyuStatisticsDOMapper userGamingMuchangwuyuStatisticsDOMapper;

    @Autowired
    private GamingMuchangwuyuBaseCfgDOMapper gamingMuchangwuyuBaseCfgDOMapper;

    @Override
    public int getDetailListCount(String tableName, String uid, String minRewardDiamonds,
                String maxRewardDiamonds, String logType, String startTime, String endTime){
        return gameRecordDOMapper.getGameRecordDetailListCount(tableName, uid, minRewardDiamonds,
                maxRewardDiamonds, logType, startTime, endTime);
    }

    @Override
    public List<GameRecordDO> getDetailList(String tableName, String uid, String minRewardDiamonds,
                String maxRewardDiamonds, String logType, String startTime, String endTime,
                String orderType, String orderBy, int offset, int limit){
        List<GameRecordDO> details = gameRecordDOMapper.getGameRecordDetailList(tableName, uid, minRewardDiamonds,
                maxRewardDiamonds, logType, startTime, endTime, orderType, orderBy, offset, limit);

        JSONArray uidList = new JSONArray();
        for(GameRecordDO detail: details){
            uidList.add(detail.getUid());
        }

        JSONObject postData = new JSONObject();
        postData.put("list_uid", uidList);
        String body = HttpInvokeUtil.httpPostJson(UrlConstant.SERVER_URL + PLAYER_LIST_URL, JSONObject.toJSONString(postData),UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);

        JSONObject data = (JSONObject)rsp.getData();
        JSONArray accountList = data.getJSONArray("account_list");
        Map<String, String> nicknameMap = new HashMap<>();
        for(int i=0; i<accountList.size(); i++){
            JSONObject accountInfo = accountList.getJSONObject(i);
            nicknameMap.put(accountInfo.getString("uid"), accountInfo.getString("user_name"));
        }

        for(GameRecordDO detail: details){
            detail.setNickname(nicknameMap.getOrDefault(detail.getUid().toString(), ""));
        }

        return details;
    }

    @Override
    public int countDaily(String uid, String minGamingDiamonds,
                          String maxGamingDiamonds, String gameType, String startTime, String endTime) {
        return gameRecordDOMapper.countGameRecordDailyData(uid, minGamingDiamonds, maxGamingDiamonds,
                gameType, startTime, endTime);
    }

    @Override
    public List<GameDailyDataDTO> listDaily(String uid, String minGamingDiamonds,
                                            String maxGamingDiamonds, String gameType, String startTime, String endTime,
                                            String orderType, String orderBy, int offset, int limit) {
        List<GameDailyDataDTO> gameDailyData = gameRecordDOMapper.listGameRecordDailyData(uid, minGamingDiamonds, maxGamingDiamonds,
                gameType, startTime, endTime, orderType, orderBy, offset, limit);
        Set<String> uidSet = new HashSet();
        for (GameDailyDataDTO item : gameDailyData) {
            uidSet.add(String.valueOf(item.getUid()));
        }
        Map<Long, String> uidMap = getNicknameMap(uidSet);
        for (GameDailyDataDTO item : gameDailyData) {
            item.setNickname(uidMap.get(item.getUid()));
        }
        return gameDailyData;
    }

    @Override
    public int getGameStatisticListCount(String gamingType, String uid, String minGamingDiamonds, String maxGamingDiamonds,
                                         String startTime, String endTime){
        return userGamingWishBottleStatisticsDOMapper.getStatisticListCount(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.userGamingStatistics), uid, minGamingDiamonds,
            maxGamingDiamonds, startTime, endTime);
    }

    @Override
    public List<UserGamingWishBottleStatisticsDO> getGameStatisticList(String gamingType, String uid,
                                                                       String minGamingDiamonds, String maxGamingDiamonds, String startTime,
                                                                       String endTime, String orderType, String orderBy, int offset, int limit){
        return userGamingWishBottleStatisticsDOMapper.getStatisticList(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.userGamingStatistics), uid, minGamingDiamonds,
                maxGamingDiamonds, startTime, endTime, orderType, orderBy, offset, limit);
    }

    @Override
    public int getTermGameDetailCount(List<String> tableList,
                                      String uid, String rewardItem, String startTime, String endTime) {
        return muchangwuyuDOMapper.getCount(tableList, uid, rewardItem, startTime, endTime);
    }

    @Override
    public List<MuchangwuyuDO> getTermGameDetailList(List<String> tableList,
                                                     String uid, String rewardItem, String startTime, String endTime, int offset, int limit) {
        List<MuchangwuyuDO> list = muchangwuyuDOMapper.getList(tableList, uid, rewardItem, startTime, endTime, offset, limit);
        Set<String> uidSet = new HashSet();
        for (MuchangwuyuDO item : list) {
            uidSet.add(String.valueOf(item.getUid()));
        }
        Map<Long, String> uidMap = getNicknameMap(uidSet);
        for (MuchangwuyuDO item : list) {
            item.setNickname(uidMap.get(item.getUid()));
        }
        return list;
    }

    private Map<Long, String> getNicknameMap(Set<String> uidSet) {

        List<String> uidList = new ArrayList(uidSet);
        List<Account> accountList = accountMapper.getNicknameList(uidList);

        Map<Long, String> uidMap = new HashMap<>();
        for (Account item : accountList) {
            uidMap.put(item.getUid(), item.getUserName());
        }
        return uidMap;
    }

    @Override
    public List<HashMap<Byte, String>> getTermGameRewardItemOptions(String gamingType) {
        return gamingMuchangwuyuBaseCfgDOMapper.getOptions(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.gamingBaseCfg));
    }

    @Override
    public int getTermGameStatisticCount(String gamingType, String uid, String startTime, String endTime) {
        return userGamingMuchangwuyuStatisticsDOMapper.getCount(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.userGamingStatistics) ,uid, startTime, endTime);
    }

    @Override
    public List<UserGamingMuchangwuyuStatisticsDO> getTermGameStatisticList(String gamingType, String uid,
                                                                            String startTime, String endTime, int offset, int limit) {
        return userGamingMuchangwuyuStatisticsDOMapper.getList(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.userGamingStatistics) ,uid, startTime, endTime, offset, limit);
    }

    @Override
    public int getTermGameRewardInfoCount(String gamingType, String termId, String itemType, String startTime, String endTime) {
        return muchangwuyuTermDOMapper.getCount(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.term), termId, itemType, startTime, endTime);
    }

    @Override
    public List<MuchangwuyuTermDO> getTermGameRewardInfoList(String gamingType, String termId, String itemType,
                                                             String startTime, String endTime, int offset, int limit) {
        return muchangwuyuTermDOMapper.getList(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.term), termId, itemType, startTime, endTime, offset, limit);
    }

    @Override
    public Map<String, Long> getTermGameRewardInfoTotal(String gamingType, String termId, String itemType,
                                                        String startTime, String endTime) {
        MuchangwuyuTermDO statistic = muchangwuyuTermDOMapper.getTotal(TermGameTableNameUtil.getTableName(gamingType, TblNameFormatEnum.term), termId, itemType, startTime, endTime);
        Map<String, Long> statisticMap = new HashMap<>();
        statisticMap.put("totalCost", statistic.getTotalCost());
        statisticMap.put("totalReward", statistic.getTotalReward());
        return statisticMap;
    }

    @Override
    public int getMuChangWuYuExchangeCount(String uid, String originType, String originId,
        String toPropType, String toPropId, String startTime, String endTime) {
        return userPropExchangeDOMapper.getCount(uid, originType, originId, toPropType, toPropId, startTime, endTime);
    }

    @Override
    public List<UserPropExchangeDO> getMuChangWuYuExchangeList(String uid, String originType, String originId,
        String toPropType, String toPropId, String startTime, String endTime, int offset, int limit) {
        return userPropExchangeDOMapper.getList(uid, originType, originId, toPropType, toPropId, startTime, endTime, offset, limit);
    }

}
